package com.example.agvctrl;

import android.graphics.BitmapFactory;
import android.os.Bundle;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class ArmCtrl extends AppCompatActivity {

    private String armStatus = "0";
    private TextView mArm;
    private String urlString = "http://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png";
    //"http://192.168.31.3/1.html";
    String ip = "10.42.0.1";
    String ip2 = "119.3.183.213";
    String IP = ip2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_arm_ctrl);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        Bundle bundle = this.getIntent().getExtras();
        IP = bundle.getString("ip");
        mArm = (TextView)findViewById(R.id.arm);
    }
    public byte[] readStream(InputStream in) throws Exception {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte data[] = new byte[1024];
        long total = 0;
        int count;
        while ((count = in.read(data)) != -1) {
            total += count;
//            mSeekBar.setProgress((int)(total*100)/fileLength);
            bos.write(data, 0, count);
        }
        bos.close();
        in.close();
        return bos.toByteArray();
    }
    private Runnable downloadRunn = new Runnable() {
        @Override
        public void run() {
            try {
                String filePath = "a.png";
                URL url = new URL(urlString);
                URLConnection con = url.openConnection();
                con.connect();
                int lengthen = con.getContentLength();
                InputStream in = con.getInputStream();
                byte[] arr = readStream(in);

                //save
                downloadRunnHandle.sendEmptyMessage(0);
            } catch (MalformedURLException e) {
                e.printStackTrace();
                //Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();
            } catch (IOException e) {
                e.printStackTrace();
                //Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    };
    private Handler downloadRunnHandle = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            Toast.makeText(getApplicationContext(), "urlOk", Toast.LENGTH_SHORT).show();
            mArm.setText("已选喷涂方案:" + armStatus);
        }
    };
    public void armCtrl(View view){
        String cmd = "0";

        switch (view.getId()) {
            case R.id.arm1:
                cmd = "1";
                break;
            case R.id.arm2:
                cmd = "2";
                break;
            case R.id.arm3:
                cmd = "3";
                break;
        }
        armStatus = cmd;
        urlString = "http://" + IP + ":5000/arm/ctrl?cmd=" + cmd + "&arg=0";
        new Thread(downloadRunn).start();
    }

}
